package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

import java.util.ArrayList;

/**
 * dao 库管,用于数据的增删改查
 */
public class OtherStudentDao implements BaseStudentDao {
    /**1.换成几集合容器
     *
     */
    private static ArrayList<Student> stus = new ArrayList<>();

    static {
        Student stu1 = new Student("heima001", "张三", "23", "1997-11-11");
        Student stu2 = new Student("heima002", "李四", "22", "1998-11-11");
        stus.add(stu1);
        stus.add(stu2);
    }
    /**
     * 添加学生方法
     * @param stu
     * @return
     */
    @Override
    public boolean addStudent(Student stu) {
        stus.add(stu);
        return true;
    }

    /**
     * 查看学生方法
     * @return
     */
    @Override
    public Student[] findAllStudent() {
        Student[] students = new Student[stus.size()];
        for (int i = 0; i < students.length; i++) {
            students[i] = stus.get(i);
        }
        return students;
    }

    /**
     * 删除学生方法
     * @param delId
     */
    @Override
    public void deleteStudentById(String delId) {
        //1.查找id所在索引位置
        int index = getIndex(delId);
        stus.remove(index);
    }

    @Override
    public int getIndex(String id){
        int index = -1;
        for (int i = 0; i < stus.size(); i++) {
            Student stu = stus.get(i);
            index = i;
            break;
        }
        return index;
    }

    /**
     * 修改学生方法
     * @param updateId
     * @param newStu
     */
    @Override
    public void updateStudent(String updateId, Student newStu) {
        //1.查找updateId,在容器中的位置
        int index = getIndex(updateId);
        stus.set(index,newStu);
    }
}
